﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections.Concurrent;
using System.Threading;
using System.ServiceModel;
using CRM.Index.Box;
using CRM.Index.IProvider;
using CRM.Entity;
using CRM.Entity.Extra;
using CRM.Entity.Page;

namespace CRM.Index.Provider
{
   
    public partial class ProviderService : IProviderService
    {
       public static ReaderWriterLockSlim  userType_rws = new ReaderWriterLockSlim();
       
        #region 插入操作
        /// <summary>
        /// 插入操作
        /// </summary>
        /// <param name="UserType"></param>
        public bool UserType_Insert(UserType userType)
        {
          try
            {
                userType_rws.EnterWriteLock();
                
                DictionaryBox.userTypeDictionary.Add(userType.UserTypeId, userType);

                DictionaryBox.userTypeUserTypeIdSort.Add(userType.UserTypeId);
                
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                
                return false;
            }
            finally
            {
                userType_rws.ExitWriteLock();
            }
        }
        #endregion

        #region 更新操作
        /// <summary>
        /// 更新操作
        /// </summary>
        /// <param name="UserType"></param>
        public bool UserType_Update(UserType userType)
        {
          try
            {
                userType_rws.EnterWriteLock();

                DictionaryBox.userTypeDictionary[userType.UserTypeId] = userType;
                
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                
                return false;
            }
            finally
            {
                userType_rws.ExitWriteLock();
            }
        }
        #endregion

        #region 删除操作
        /// <summary>
        /// 删除操作
        /// </summary>
        /// <param name="UserType"></param>
        public bool UserType_Delete(UserType userType)
        {
          try
            {
                userType_rws.EnterWriteLock();

                DictionaryBox.userTypeDictionary.Remove(userType.UserTypeId);

                DictionaryBox.userTypeUserTypeIdSort.Remove(userType.UserTypeId);
                
                return true;
            }  
            catch (Exception e)
            {
                Console.WriteLine(e.Message);

                return false;
            }
            finally
            {
                userType_rws.ExitWriteLock();
            }
        }
        #endregion
        
        #region 根据主键获取一条
        /// <summary>
        /// 根据主键获取一条
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public UserType UserType_Single(Int64 id)
        {
            try
            {
                userType_rws.EnterReadLock();

                return DictionaryBox.userTypeDictionary[id];
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
            finally
            {
                userType_rws.ExitReadLock();
            }
        }
        #endregion
        
        #region 获取TopK条
        /// <summary>
        /// 获取TopK条
        /// </summary>
        /// <param name="top"></param>
        public UserTypePage UserType_Top(int top)
        {
            try
            {
                 userType_rws.EnterReadLock();

                var  userTypePage = new  UserTypePage(){ ListEntity = new List<UserType>() };

                var skip = DictionaryBox.userTypeUserTypeIdSort.Count - top < 0 ? 0 : DictionaryBox.userTypeUserTypeIdSort.Count - top;

                var listID = DictionaryBox.userTypeUserTypeIdSort.Skip(skip).Take(top).ToList();

                for (int i = listID.Count - 1; i >= 0; i--)
                {
                    userTypePage.ListEntity.Add(DictionaryBox.userTypeDictionary[listID[i]]);
                }

                userTypePage.RecordCount = DictionaryBox.userTypeUserTypeIdSort.Count;

                return userTypePage;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
            finally
            {
                 userType_rws.ExitReadLock();
            }
        }
        #endregion

        #region 分页获取条数
        /// <summary>
        /// 分页获取条数
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public UserTypePage UserType_Page(PageInfo info)
        {
            try
            {
                userType_rws.EnterReadLock();

                var userTypePage = new UserTypePage() { ListEntity = new List<UserType>() };

                var skip = DictionaryBox.userTypeUserTypeIdSort.Count - (info.PageIndex * info.PageSize);

                info.PageSize = skip < 0 ? skip + info.PageSize : info.PageSize;

                var listID = DictionaryBox.userTypeUserTypeIdSort.Skip(skip).Take(info.PageSize).ToList();

                for (int i = listID.Count - 1; i >= 0; i--)
                {
                    userTypePage.ListEntity.Add(DictionaryBox.userTypeDictionary[listID[i]]);
                }

                userTypePage.RecordCount = DictionaryBox.userTypeUserTypeIdSort.Count;

                return userTypePage;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
            finally
            {
                userType_rws.ExitReadLock();
            }
        } 
        #endregion
        
        #region 分页获取条数
        /// <summary>
        /// 分页获取条数
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public UserTypeExtraPage UserTypeExtra_Page(PageInfo info)
        {
            try
            {
                userType_rws.EnterReadLock();

                var userTypeExtaPage = new UserTypeExtraPage() { ListEntity = new List<UserTypeExtra>() };

                var skip = DictionaryBox.userTypeUserTypeIdSort.Count - (info.PageIndex * info.PageSize);

                info.PageSize = skip < 0 ? skip + info.PageSize : info.PageSize;

                var listID = DictionaryBox.userTypeUserTypeIdSort.Skip(skip).Take(info.PageSize).ToList();

                for (int i = listID.Count - 1; i >= 0; i--)
                {
                    var single = DictionaryBox.userTypeDictionary[listID[i]];

                    var userTypeExtra = new UserTypeExtra()
                    {
                        UserTypeId=single.UserTypeId,
                        TypeName=single.TypeName,
                        Disable=single.Disable,
                    };

                    userTypeExtaPage.ListEntity.Add(userTypeExtra);
                }

                userTypeExtaPage.RecordCount = DictionaryBox.userTypeUserTypeIdSort.Count;

                return userTypeExtaPage;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
            finally
            {
                userType_rws.ExitReadLock();
            }
        }
        #endregion
    }
}
